package com.freestyle.nettychat.chat.dto;

import com.freestyle.netty.easynetty.common.MD5Utils;
import com.freestyle.nettychat.chat.CodeConsts;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by rocklee on 2022/2/4 21:26
 */
public class AttachmentMessage extends ChatMessage<byte[]> {
  public static AttachmentMessage fromAttachment(long id,String chatRoom,String uuid,File filePath) throws IOException {
    ChatMessageProperties properties=new ChatMessageProperties();
    properties.setId(id);
    properties.setUuid(uuid);
    properties.setChatRoom(chatRoom);
    properties.setSClass(CodeConsts.MessageType.attachment.toString());
    properties.setExtParams("file",filePath.getName());
    AttachmentMessage message=new AttachmentMessage();
    message.setProperties(properties);
    byte[] content=FileUtils.readFileToByteArray(filePath);
    message.setData(content);
    properties.setMD5(MD5Utils.encryptMD5(content));
    return message;
  }
  public static AttachmentMessage fromMessage(ChatMessage message){
    AttachmentMessage attachmentMessage=new AttachmentMessage();
    attachmentMessage.setProperties(message.getProperties());
    attachmentMessage.getProperties().setSClass(CodeConsts.MessageType.attachment.toString());
    attachmentMessage.setData(message.getData().toString().getBytes());
    return attachmentMessage;
  }
}
